package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.impl.DestinationServiceImpl;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("destinations")
public class DestinationController {
    @Autowired
   private IStrategyService strategyService;

    @Autowired
    private ITravelService travelService;

    @Autowired
    private DestinationServiceImpl destinationService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @GetMapping("/hotRegion")
    public Object hotRegion(){
       //查询全部热门区域
        List<Region> regions=destinationService.queryForHost();
        return JsonResult.success(regions);
    }
    @GetMapping("/search")
    public Object search(String regionId){
        if(!StringUtils.hasLength(regionId)){
            return null;
        }
        List<Destination> list = new ArrayList<>();
        if("-1".equals(regionId)){
            //查询国内目的地
           list=destinationService.queryForParentName();
        }else {
            //查询非国内目的地
            list = destinationService.queryDestinationByRegionId(regionId);
        }
        //遍历目的地集合
        for (Destination destination : list) {
            //查询每个目的地的子目的地集合
            List<Destination> children=destinationService.findByParentId(destination.getId());
            //设置到目的地的子地区属性中
            destination.setChildren(children);
        }
        //返回数据
        return JsonResult.success(list);
    }
    @GetMapping("/toasts")
    public Object toasts(String destId){
        return JsonResult.success(destinationService.queryToasts(destId));
    }
    @GetMapping("/catalogs")
    public Object catalogs(String destId) {
       List<StrategyCatalog> catalogs = strategyCatalogService.findByDestId(destId);
       return JsonResult.success(catalogs);
    }
    @GetMapping("/strategies/viewnumTop3")
    public Object strategiesViewNumTop3(String destId) {
        List<Strategy> strategies = strategyService.queryViewNum(destId);
        return JsonResult.success(strategies);
    }
    @GetMapping("/travels/viewnumTop3")
    public Object travelsViewNumTop3(String destId) {
        List<Travel> travels = travelService.queryViewNum(destId);
        return JsonResult.success(travels);
    }
    @GetMapping("/list")
    public Object list() {
        return JsonResult.success(destinationService.list());
    }
}
